package quizman.tdg;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import org.dsrg.ts.threadLocal.DbRegistry;


public class QuizTDG {

	public static void delete(String quizName) throws Exception {
		try {
			if(quizName==null || quizName.equals(""))
				throw new IllegalArgumentException("Invalid argument to QuizTDG.delete.");
			Connection db = DbRegistry.getDbConnection();
			PreparedStatement dbStatement =
				db.prepareStatement("DELETE FROM Quiz WHERE Quiz_Name LIKE ?");
			dbStatement.setString(1, quizName);
			dbStatement.executeUpdate();
		} catch (Exception e) {
			String msg = "Error in QuizTDG.delete";
			throw new Exception(msg, e);
		}
	}
	
	public static void insert(String quizName, int length, Date startDate, Date endDate) throws Exception {
		try {
			if(quizName==null || quizName.equals("") || startDate.after(endDate))
				throw new IllegalArgumentException("Invalid argument to QuizTDG.insert.");
			Connection db = DbRegistry.getDbConnection();
			PreparedStatement dbStatement =
				db.prepareStatement("INSERT INTO Quiz (Quiz_Name, Start, End, Length) values(?, ?, ?, ?)");
			dbStatement.setString(1, quizName);
			dbStatement.setDate(2, startDate);
			dbStatement.setDate(3, endDate);
			dbStatement.setInt(4, length);
			dbStatement.executeUpdate();
		} catch (Exception e) {
			String msg = "Error in QuizTDG.insert";
			throw new Exception(msg, e);
		}
	}

	public static void update(String quizName, int length, Date startDate, Date endDate)  throws Exception {
		try {
			if(quizName==null || quizName.equals("") || startDate.after(endDate))
				throw new IllegalArgumentException("Invalid argument to QuizTDG.update.");
			Connection db = DbRegistry.getDbConnection();
			PreparedStatement dbStatement =
				db.prepareStatement("UPDATE Quiz SET Start = ?, End = ?, Length = ? WHERE Quiz_Name LIKE ?");
			dbStatement.setDate(1, startDate);
			dbStatement.setDate(2, endDate);
			dbStatement.setInt(3, length);
			dbStatement.setString(4, quizName);
			dbStatement.executeUpdate();
		} catch (Exception e) {
			String msg = "Error in QuizTDG.update";
			throw new Exception(msg, e);
		}
	}
	
	public static ResultSet find(String name) throws Exception
	{
		try {
			if(name==null || name.equals(""))
				throw new IllegalArgumentException("Invalid argument to QuizTDG.find.");
            Connection db = DbRegistry.getDbConnection();
            PreparedStatement dbStatement =
                    db.prepareStatement("SELECT * from Quiz where Quiz_Name LIKE ?");

            dbStatement.setString(1, name);
            ResultSet rs = dbStatement.executeQuery();
            return rs;
		} 
		catch (Exception e) {
            String msg = "Error in QuizTDG.find";
            throw new Exception(msg, e);
		}
	}
	public static ResultSet findAll() throws Exception
	{
		try {
            Connection db = DbRegistry.getDbConnection();
            PreparedStatement dbStatement =
                    db.prepareStatement("SELECT * from Quiz");

            ResultSet rs = dbStatement.executeQuery();
            return rs;
		} 
		catch (Exception e) {
            String msg = "Error in QuizTDG.findAll";
            throw new Exception(msg, e);
		}
	}

}
